﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Business.Stores
{
    public class ProductManager
    {
        private MyCommerceEntities vContext;

        public ProductManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public ProductManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<Product> GetProducts()
        {
            List<Product> colProduct = new List<Product>();

            var result = from st in vContext.Product
                         .Include("Classifier")
                         .Include("Classifier1")
                         .Include("Classifier2")
                         select st;
            if (result != null)
                colProduct = new List<Product>(result.AsEnumerable<Product>());

            return colProduct;
        }
        
        public Product GetProductByID(long IdProduct)
        {
            Product objProduct = new Product();
            var result = (from st in vContext.Product
                          where st.IdProduct == IdProduct
                          select st).First();
            objProduct = (Product)result;
            return objProduct;

        }

        public bool ExistProductInStock(long IdProduct)
        {
            var result = (from st in vContext.ProductStock
                         where st.IdProduct == IdProduct
                         select st).FirstOrDefault();
            if (result != null)
                if (result.Quantity > 0)
	            {
                    return true;
	            }
                else
                {
                    return false;
                }
            else
                return false;
        }

        public List<Product> GetProductByModel(string Model)
        {
            if (string.IsNullOrEmpty(Model))
            {
                return null;
            }
            List<Product> colProduct = new List<Product>();
            var result = (from st in vContext.Product
                          .Include("Classifier")
                          where st.Model.Contains(Model)
                          select st);
            List<Product> resultList = new List<Product>(result.AsEnumerable<Product>());
            if (resultList != null)
                foreach (Product item in resultList)
                {
                    if (ExistProductInStock(item.IdProduct))
                    {
                        var resultStock = (from st in vContext.ProductStock
                                      where st.IdProduct == item.IdProduct
                                      select st).FirstOrDefault();
                        item.OtherDescription = resultStock.Quantity.ToString();
                        colProduct.Add(item);
                    }
                }



            return colProduct;

        }

        /// <summary>
        /// Busca el modelo con el texto completo.
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public Product GetProductByModelEntireWord(string Model)
        {
            Product objProduct = new Product();

            if (string.IsNullOrEmpty(Model))
            {
                return null;
            }
            List<Product> colProduct = new List<Product>();
            var result = (from st in vContext.Product
                          .Include("Classifier")
                          where st.Model.ToUpper() == Model.ToUpper()
                          select st).FirstOrDefault();
            Product resultObj = result;
            if (resultObj != null)
            {
                if (ExistProductInStock(resultObj.IdProduct))
                {
                    var resultStock = (from st in vContext.ProductStock
                                       where st.IdProduct == resultObj.IdProduct
                                       select st).FirstOrDefault();
                    resultObj.OtherDescription = resultStock.Quantity.ToString();
                    objProduct = (Product)resultObj;
                }
                else
                {
                    objProduct = null;
                }
            }
            return objProduct;
        }
        
        public void SaveProduct(Product objProduct)
        {
            var validateResult = (from cl in vContext.Product
                                  where (cl.Model.CompareTo(objProduct.Model) == 0 && cl.IdcMark != objProduct.IdcMark)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un producto con este modelo.");
            }

            vContext.AddToProduct(objProduct);
            vContext.SaveChanges();
        }

        public void UpdateProduct(Product objProduct)
        {
            var validateResult = (from cl in vContext.Product
                                  where (cl.Model.CompareTo(objProduct.Model) == 0 && cl.IdcMark != objProduct.IdcMark && cl.IdProduct != objProduct.IdProduct)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un producto con este modelo.");
            }

            var vResult = (from st in vContext.Product
                           where st.IdProduct == objProduct.IdProduct
                           select st).First();


            vResult.IdcCategory = objProduct.IdcCategory;
            vResult.IdcMark = objProduct.IdcMark;
            vResult.IdcProductType = objProduct.IdcProductType;
            vResult.Model = objProduct.Model;
            vResult.Description = objProduct.Description;
            vResult.Color = objProduct.Color;
            vResult.ProductLink = objProduct.ProductLink;
            vResult.WithGuarantee = objProduct.WithGuarantee;
            vResult.OtherDescription = objProduct.OtherDescription;

            vContext.SaveChanges();
        }

        public void DeleteProduct(long IdProduct)
        {
            var vResult = (from st in vContext.Product
                           where st.IdProduct == IdProduct
                           select st).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

    }
}
